<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of notesModelClass
 *
 * @author nstevens
 */
class notesModelClass extends commonModel {

    public function __construct() {
        
    }

    /**
     * Called by all existing notes related buttons
     * @return null
     */
    public function processExistingNotes() {
        //print_r($_POST);
        if (($index = getIndexClickedGenericList("button", 20)) != -1) {
            if (strcmp("Mark as complete", $_POST["button$index"]) === 0) {
                return $this->processNotesMarkAsComplete($index);
            } else if (strcmp("Update Notes", $_POST["button$index"]) === 0) {
                return $this->processEditNotes($index);
            } else if (strcmp("Save Grade", $_POST["button$index"]) === 0 || strcmp("Save All", $_POST["button$index"])) {
                if (!$this->processSaveGrade($index)) {
                    return NULL;
                }
                $url = $_SERVER['HTTP_REFERER'];
                return "location:$url&update=Grade updated Successfully&activeDiv=grading";
            }
        } 
        else if (isset($_POST['buttonAll'])) {
            for ($i = 0; $i < sizeof(getGradingCategoryArray()); $i++) {
                if (!$this->processSaveGrade($i)) {
                    return NULL;
                }
            }
            $url = $_SERVER['HTTP_REFERER'];
            return "location:$url&update=Grade(s) updated Successfully&activeDiv=grading";
        }
    }

    /**
     * Updates the given note to completed
     * @param type $index index of the button that was selected. There may be many buttons in a single form
     * @return the redirect url or NULL if there was a failure
     */
    public function processNotesMarkAsComplete($index) {
        $notes = $_POST['notes' . $index . ''];
        $wbsId = $_POST['wbs' . $index . ''];
        $lastUpdated = date("Y-m-d");
        $updateResult = executeUpdate("UPDATE notes SET status=\"completed\", lastUpdated='$lastUpdated' WHERE wbsId='$wbsId' AND notes='$notes'");
        if ($updateResult) {
            return 'location:' . $this->getUrlSource() . '&update=Note Marked as Complete';
        }
        return NULL;
    }

    /**
     * Updates the existing notes row in the db
     * @param type $index index of the button that was selected. There may be many buttons in a single form
     * @return the redirect url or NULL if there was a failure
     */
    public function processEditNotes($index) {
        //print_r($_POST);
        $notes = $_POST['textFieldNotes' . $index . ''];
        $notesID = $_POST['notesID' . $index . ''];
        $lastUpdated = date("Y-m-d");
        $updateResult = executeUpdate("UPDATE notes SET notes='$notes', lastUpdated='$lastUpdated' WHERE notesId=$notesID");
        if ($updateResult) {
            //echo "UPDATE notes SET notes='$notes', lastUpdated='$lastUpdated' WHERE notesId=$notesID";
            return 'location:' . $this->getUrlSource() . '&update=Notes Updated Successfully';
        }
        return NULL;
    }

    /**
     * Saves a single grade for the given index
     * @param type $index index of the button that was selected. There may be many buttons in a single form
     * @return true=success, false=failure
     */
    protected function processSaveGrade($index) {
        $category = $_POST['category' . $index . ''];
        $score = $_POST['scoreDropdown' . $index . ''];
        $comments = $_POST['commentsTextField' . $index . ''];
        $wbsID = $_POST['wbsID'];
        //echo $wbsID;
        $personID = $_POST['personID'];
        //see if there is an entry for this project and category
        $projectResult = executeSelect("SELECT * FROM notes WHERE wbsID='$wbsID' AND subtype='$category'and personId=$personID");
        if ($projectResult && $projectResult->num_rows > 0) {
            //update
            //echo "UPDATE notes set value='$score', notes='$comments' WHERE wbsID='$wbsID' AND subtype='$category' AND personID=$personID";
            $updateResult = executeUpdate("UPDATE notes set value='$score', notes='$comments' WHERE wbsID='$wbsID' AND subtype='$category' AND personID=$personID");
        } else {
            //insert
//            echo "INSERT into notes (value, notes, wbsID, type, subType, personID) 
//                                                          values ('$score','$comments','$wbsID', 'grading', '$category', '$personID')";
            $updateResult = executeUpdate("INSERT into notes (value, notes, wbsID, type, subType, personID) 
                                                          values ('$score','$comments','$wbsID', 'grading', '$category', '$personID')");
        }
        if ($updateResult) {
            return true;
        }
        return false;
    }

}

?>
